package net.tinyportal.portal;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import net.tinyportal.tools.spring.PortletHolderFactoryBean;

public class User {
	
	@Autowired PortletHolderFactoryBean portletHolderFactoryBean;

	private Map<String, PortletHolder> portletsHolder = new HashMap<String, PortletHolder>();

	public void setPortletHolder(String portletName, PortletHolder holder) {
		portletsHolder.put(portletName, holder);
	}

	public PortletHolder getHolder(String portletName) {
		if (portletsHolder.containsKey(portletName)) {
			return portletsHolder.get(portletName);
		} else {
			PortletHolder holder;
			try {
				holder = portletHolderFactoryBean.getObject();
				portletsHolder.put(portletName, holder);
				return holder;
			} catch (Exception e) {
				return null;
			}
		}
	}

	public PortletHolder getHolder(HttpServletRequest request) {
		String portletName = (String)request.getAttribute("net.tinyportal.spring.portlet");
		return getHolder(portletName);
	}
}
